package com.ruoyi.manage.organization.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.ObjUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import com.ruoyi.manage.organization.domain.OcOrderRepayment;
import com.ruoyi.manage.organization.domain.vo.OcOrderRepaymentVo;
import com.ruoyi.manage.organization.service.IOcOrderRepaymentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 订单还款记录Controller
 *
 * @author ruoyi
 * @date 2025-08-20
 */
@Api(tags="订单还款记录控制器")
@RestController
@RequestMapping("/manage/repayment" )
public class OcOrderRepaymentController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IOcOrderRepaymentService ocOrderRepaymentService;

    /**
     * 查询订单还款记录列表
     */
    @ApiOperation(value = "查询订单还款记录",notes = "订单还款记录管理")
    @PreAuthorize("@ss.hasPermi('manage:repayment:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(OcOrderRepaymentVo ocOrderRepayment,QueryWrapper<OcOrderRepayment> queryWrapper) {
        setQueryCondition(ocOrderRepayment,queryWrapper);
        startPage();
        List<OcOrderRepayment> list = ocOrderRepaymentService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(OcOrderRepaymentVo ocOrderRepayment, QueryWrapper<OcOrderRepayment> queryWrapper) {
        if(StrUtil.isNotBlank(ocOrderRepayment.getVersion())){
            queryWrapper.eq(TABLE_PREFIX+"version" ,ocOrderRepayment.getVersion());
        }
        if(ocOrderRepayment.getDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"deleted" ,ocOrderRepayment.getDeleted());
        }
        if(StrUtil.isNotBlank(ocOrderRepayment.getMachNo())){
            queryWrapper.eq(TABLE_PREFIX+"mach_no" ,ocOrderRepayment.getMachNo());
        }
        if(StrUtil.isNotBlank(ocOrderRepayment.getOrderNo())){
            queryWrapper.eq(TABLE_PREFIX+"order_no" ,ocOrderRepayment.getOrderNo());
        }
        if(ocOrderRepayment.getRepaymentAmount() != null){
            queryWrapper.eq(TABLE_PREFIX+"repayment_amount" ,ocOrderRepayment.getRepaymentAmount());
        }
        if(StrUtil.isNotBlank(ocOrderRepayment.getOverdueFlag())){
            queryWrapper.eq(TABLE_PREFIX+"overdue_flag" ,ocOrderRepayment.getOverdueFlag());
        }
        if(ocOrderRepayment.getOverdueAmount() != null){
            queryWrapper.eq(TABLE_PREFIX+"overdue_amount" ,ocOrderRepayment.getOverdueAmount());
        }
        if(ocOrderRepayment.getBadDebtsAmount() != null){
            queryWrapper.eq(TABLE_PREFIX+"bad_debts_amount" ,ocOrderRepayment.getBadDebtsAmount());
        }
        if(ocOrderRepayment.getRepaymentTime() != null){
            queryWrapper.eq(TABLE_PREFIX+"repayment_time" ,ocOrderRepayment.getRepaymentTime());
        }
        if(ocOrderRepayment.getFeedbackTime() != null){
            queryWrapper.eq(TABLE_PREFIX+"feedback_time" ,ocOrderRepayment.getFeedbackTime());
        }
        if(StrUtil.isNotBlank(ocOrderRepayment.getClearFlag())){
            queryWrapper.eq(TABLE_PREFIX+"clear_flag" ,ocOrderRepayment.getClearFlag());
        }
        if(StrUtil.isNotBlank(ocOrderRepayment.getRepaymentType())){
            queryWrapper.eq(TABLE_PREFIX+"repayment_type" ,ocOrderRepayment.getRepaymentType());
        }
        if(ocOrderRepayment.getPremiumAmount() != null){
            queryWrapper.eq(TABLE_PREFIX+"premium_amount" ,ocOrderRepayment.getPremiumAmount());
        }
        if(ocOrderRepayment.getPremiumDate() != null){
            queryWrapper.eq(TABLE_PREFIX+"premium_date" ,ocOrderRepayment.getPremiumDate());
        }
        if(ocOrderRepayment.getPeriods() != null){
            queryWrapper.eq(TABLE_PREFIX+"periods" ,ocOrderRepayment.getPeriods());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出订单还款记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:repayment:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, OcOrderRepaymentVo ocOrderRepayment,QueryWrapper<OcOrderRepayment> queryWrapper) {
        if (CollUtil.isEmpty( ocOrderRepayment.getIds())){
            setQueryCondition(ocOrderRepayment,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",ocOrderRepayment.getIds());
        }
        List<OcOrderRepayment> list = ocOrderRepaymentService.list(queryWrapper);
        ExcelUtil<OcOrderRepayment> util = new ExcelUtil<OcOrderRepayment>(OcOrderRepayment. class);
        util.exportExcel(response,list, "repayment");
    }

    /**
     * 获取订单还款记录详细信息
     */
    @ApiOperation(value = "获取订单还款记录",notes = "订单还款记录管理")
    @PreAuthorize("@ss.hasPermi('manage:repayment:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ocOrderRepaymentService.queryGetById(id));
    }

    /**
     * 新增订单还款记录
     */
    @ApiOperation(value = "新增订单还款记录",notes = "订单还款记录管理")
//    @PreAuthorize("@ss.hasPermi('manage:repayment:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody OcOrderRepayment ocOrderRepayment) {
        return toAjax(ocOrderRepaymentService.addOcOrderRepayment(ocOrderRepayment) ? 1 : 0);
    }

    /**
     * 修改订单还款记录
     */
    @ApiOperation(value = "修改订单还款记录",notes = "订单还款记录管理")
    @PreAuthorize("@ss.hasPermi('manage:repayment:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody OcOrderRepayment ocOrderRepayment) {
        return toAjax(ocOrderRepaymentService.updateOcOrderRepayment(ocOrderRepayment) ? 1 : 0);
    }

    /**
     * 删除订单还款记录
     */
    @ApiOperation(value = "删除订单还款记录",notes = "订单还款记录管理")
    @PreAuthorize("@ss.hasPermi('manage:repayment:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ocOrderRepaymentService.deleteOcOrderRepaymentByIds(ids) ? 1 : 0);
    }
}
